//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.framework.web.codetable;

import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.service.CacheService;
import com.hyjx.framework.service.ConfigManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;


public class CodeTableManager {
    private static Logger logger = Logger.getLogger(CodeTableManager.class);
//    private static IBaseDao dao = (IBaseDao)SpringBeanUtil.getBean("baseDao");
    private static Map<String, CodeTableInfo> codeTableMap = new HashMap();

    public CodeTableManager() {
    }

    public static void init() {
        logger.debug("开始加载代码表" + new Date());
        codeTableMap = new HashMap();
        Map<String, String> configMap = ConfigManager.getAllConfig();
        Iterator var2 = configMap.keySet().iterator();

        while(true) {
            String key;
            String sql;
            while(true) {
                do {
                    if (!var2.hasNext()) {
                        List<CodeTableInfo> tableList = null ; //dao.queryObjectList("codetable.queryTableList", (Object)null);//TODO 数据接口
                        Iterator var13 = tableList.iterator();

                        while(var13.hasNext()) {
                            CodeTableInfo codeTable = (CodeTableInfo)var13.next();
                            logger.debug("开始加载代码表" + codeTable.getCodetable());
                            codeTableMap.put(codeTable.getCodetable(), codeTable);
                            logger.debug("结束加载代码表" + codeTable.getCodetable());
                        }

                        List<CodeBean> codeList = null ;//dao.queryObjectList("codetable.queryCodeList", (Object)null); //TODO 数据接口
                        Iterator var16 = codeList.iterator();

                        while(var16.hasNext()) {
                            CodeBean codeBean = (CodeBean)var16.next();
                            CodeTableInfo codeTable = (CodeTableInfo)codeTableMap.get(codeBean.getCodetable());
                            if (codeTable == null) {
                                logger.error("加载代码表" + codeBean.getCodetable() + "，在管理代码表中没有配置。");
                            } else {
                                codeTable.getCodeList().add(codeBean);
                            }
                        }

                        logger.debug("所有代码表加载结束：" + new Date());
                        return;
                    }

                    key = (String)var2.next();
                } while(!key.startsWith("sys.codetable."));

                sql = (String)configMap.get(key);
                int temp = sql.indexOf("???");
                if (temp != -1) {
                    break;
                }

                logger.debug("开始加载代码表" + key);

                try {
                    List list = null ; //dao.queryObjectList("codetable.querySqlTableList", sql);
                    CodeTableInfo codeTable = new CodeTableInfo();
                    codeTable.setCodeList(list);
                    codeTable.setCodetable(key);
                    codeTableMap.put(key, codeTable);
                    String shortKey = key.substring(14);
                    CodeTableInfo codeTable2 = new CodeTableInfo();
                    codeTable2.setCodeList(list);
                    codeTable2.setCodetable(shortKey);
                    codeTableMap.put(shortKey, codeTable2);
                } catch (Exception var9) {
                    logger.error(var9.getMessage());
                    continue;
                }

                logger.debug("结束加载代码表" + key);
                break;
            }

            CacheService.set(key, sql);
        }
    }

    public static List<CodeBean> getTableList(String tableName) {
        if (tableName != null && !"".equals(tableName)) {
            CodeTableInfo codeTable = (CodeTableInfo)codeTableMap.get(tableName);
            if (codeTable == null) {
                return null;
            } else {
                List<CodeBean> list = codeTable.getCodeList();
                List<CodeBean> list2 = new ArrayList();
                List<CodetableModel> list3 = new ArrayList();
                Iterator var6 = list.iterator();

                while(true) {
                    Object cb;
                    label51:
                    do {
                        while(var6.hasNext()) {
                            cb = var6.next();
                            if (cb instanceof CodeBean) {
                                continue label51;
                            }

                            if (cb instanceof CodetableModel) {
                                if ("#".equals(((CodetableModel)cb).getParentcode()) || ((CodetableModel)cb).getParentcode() == null) {
                                    list3.add((CodetableModel)cb);
                                }

                                list2.clear();
                                Iterator var8 = list3.iterator();

                                while(var8.hasNext()) {
                                    CodetableModel cm = (CodetableModel)var8.next();
                                    CodeBean cb1 = new CodeBean();
                                    cb1.setCode(cm.getCode());
                                    cb1.setCodetable(cm.getCodetable());
                                    cb1.setName(cm.getName());
                                    if (StringUtils.isNoneEmpty(new CharSequence[]{cm.getOrderid()})) {
                                        cb1.setOrderid(Integer.parseInt(cm.getOrderid()));
                                    }

                                    cb1.setParentCode(cm.getParentcode());
                                    cb1.setValidflag(cm.getValidflag());
                                    list2.add(cb1);
                                }
                            }
                        }

                        return list2;
                    } while(!"#".equals(((CodeBean)cb).getParentCode()) && !StringUtils.isEmpty(((CodeBean)cb).getParentCode()));

                    list2.add((CodeBean)cb);
                }
            }
        } else {
            return null;
        }
    }

    public static String getNameByCode(String tableName, String code) {
        if (tableName != null && !"".equals(tableName)) {
            CodeTableInfo codeTable = (CodeTableInfo)codeTableMap.get(tableName);
            if (codeTable == null) {
                return null;
            } else if (code == null) {
                return null;
            } else {
                List<CodeBean> list = codeTable.getCodeList();
                Iterator var5 = list.iterator();

                while(var5.hasNext()) {
                    CodeBean bean = (CodeBean)var5.next();
                    if (bean.getCode().equals(code)) {
                        return bean.getName();
                    }
                }

                return null;
            }
        } else {
            return null;
        }
    }

    public static String getCodeByName(String tableName, String name) {
        if (tableName != null && !"".equals(tableName)) {
            CodeTableInfo codeTable = (CodeTableInfo)codeTableMap.get(tableName);
            if (codeTable == null) {
                return null;
            } else if (name == null) {
                return null;
            } else {
                List<CodeBean> list = codeTable.getCodeList();
                Iterator var5 = list.iterator();

                while(var5.hasNext()) {
                    CodeBean bean = (CodeBean)var5.next();
                    if (bean.getName().equals(name)) {
                        return bean.getCode();
                    }
                }

                return null;
            }
        } else {
            return null;
        }
    }

    public static List<CodeBean> getSubCodeList(String tableName, String parentcode) {
        if (tableName != null && !"".equals(tableName)) {
            CodeTableInfo codeTable = (CodeTableInfo)codeTableMap.get(tableName);
            if (codeTable == null) {
                return null;
            } else {
                List<CodeBean> list = codeTable.getCodeList();
                List<CodeBean> resultList = new ArrayList();
                Iterator var6 = list.iterator();

                while(var6.hasNext()) {
                    Object bean = var6.next();
                    CodeBean cb;
                    if (bean instanceof CodeBean) {
                        cb = (CodeBean)bean;
                        if (cb.getParentCode() != null && cb.getParentCode().equals(parentcode) && "1".equals(cb.getValidflag())) {
                            resultList.add(cb);
                        }
                    } else if (bean instanceof CodetableModel) {
                        cb = new CodeBean();
                        CodetableModel cm = (CodetableModel)bean;
                        cb.setCode(cm.getCode());
                        cb.setCodetable(cm.getCodetable());
                        cb.setName(cm.getName());

                        try {
                            cb.setOrderid(Integer.parseInt(cm.getOrderid()));
                        } catch (Exception var10) {
                        }

                        cb.setParentCode(cm.getParentcode());
                        cb.setValidflag(cm.getValidflag());
                        if (cb.getParentCode() != null && cb.getParentCode().equals(parentcode) && "1".equals(cb.getValidflag())) {
                            resultList.add(cb);
                        }
                    }
                }

                return resultList;
            }
        } else {
            return null;
        }
    }

    public static Map<String, CodeTableInfo> getCodeTableMap() {
        return (Map)((HashMap)codeTableMap).clone();
    }

    public static boolean reloadCodeTable(String codeTable) {
        logger.debug("重新加载代码表开始" + codeTable);

        try {
            String sql = null;
            Map<String, String> configMap = ConfigManager.getAllConfig();
            Iterator var4 = configMap.keySet().iterator();

            while(var4.hasNext()) {
                String key = (String)var4.next();
                if (("sys.codetable." + codeTable).equalsIgnoreCase(key)) {
                    sql = (String)configMap.get(key);
                }
            }

            if (sql != null) {
                List list = null  ;// dao.queryObjectList("codetable.querySqlTableList", sql);//TODO 数据接口
                CodeTableInfo codeTableInfo = new CodeTableInfo();
                codeTableInfo.setCodeList(list);
                codeTableInfo.setCodetable(codeTable);
                codeTableMap.put(codeTable, codeTableInfo);
            } else {
                CodetableModel codetablemodel = new CodetableModel();
                codetablemodel.setCodetable(codeTable);
                List list = null ; // dao.queryObjectList("codetable.queryList", codetablemodel); //TODO 数据接口
                CodeTableInfo codeTableInfo = (CodeTableInfo)codeTableMap.get(codeTable);
                if (codeTableInfo == null) {
                    logger.error("加载代码表" + codeTable + "，在代码表明细中不存在。");
                } else {
                    codeTableInfo.setCodeList(list);
                }
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }

        logger.debug("重新加载代码表结束" + codeTable);
        return true;
    }

    public static void addCodeTable(String key, CodeTableInfo cti) {
        codeTableMap.put(key, cti);
    }
}
